package cn.wiseda.finance.services;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.wiseda.finance.dao.CashFlowDao;
import cn.wiseda.finance.dao.util.page.Page;
import cn.wiseda.finance.dao.util.page.Pager;
import cn.wiseda.finance.entity.CashFlow;
import cn.wiseda.finance.entity.CashFlowByBank;

@Component
@Transactional
public class CashFlowServices {

	@Resource
	private CashFlowDao cashFlowDao;

	public boolean insertCashFlow(CashFlow cashFlow) {
		return cashFlowDao.writerCashFlow(cashFlow);
	}

	public List<CashFlow> listCashFlow() {
		return cashFlowDao.getCashFlow();
	}

	public Pager<CashFlow> pageCashFlowByBank(int bankId, int pageNo, int pageSize) {
		Pager<CashFlow> page = cashFlowDao.pageCashFlowByBank(bankId, pageNo, pageSize);
		return page;
	}

	public List<CashFlowByBank> listCashFlowByBank() {
		return cashFlowDao.listCashFlowByBanks();
	}

	public List<CashFlow> listCashFlowByBank(int bankId) {
		return cashFlowDao.listCashFlowByBank(bankId);
	}

	public List<CashFlow> settleAccounts(int bankId) {
		List<CashFlow> list = cashFlowDao.listCashFlowByBank(bankId);
		if (list == null || list.size() == 0)
			return null;
		List<CashFlow> cashFlows = new ArrayList<>();
		CashFlow cf0 = list.get(0);
		Float d0 = cf0.getDebit();
		Float c0 = cf0.getCreditor();
		Float sumMoney = c0 - d0;
		System.out.println(cf0 + "\t" + sumMoney);
		cf0.setSettle(sumMoney);
		cf0.setStatus("1");
		cashFlows.add(cf0);
		int length = list.size();
		for (int i = 1; i < length; i++) {
			CashFlow cashFlow = list.get(i);

			Float debit = cashFlow.getDebit();// 借方
			Float creditor = cashFlow.getCreditor();// 贷方
			sumMoney = sumMoney - debit + creditor;
			System.out.println(cashFlow + "\t" + sumMoney);
			cashFlow.setSettle(sumMoney);
			cashFlow.setStatus("1");
			cashFlows.add(cashFlow);
		}
		return cashFlows;
	}

	public void settleAccountsByBank(int bankId) {
		List<CashFlow> list = cashFlowDao.listCashFlowByBank(bankId);
		if (list == null || list.size() == 0)
			return;
		List<CashFlow> cashFlows = new ArrayList<>();
		CashFlow cf0 = list.get(0);
		Float d0 = cf0.getDebit();
		if (d0 == null) {
			d0 = 0f;
		}
		Float c0 = cf0.getCreditor();
		if (c0 == null) {
			c0 = 0f;
		}
		Float sumMoney = c0 - d0;
		System.out.println(cf0 + "\t" + sumMoney);
		cf0.setSettle(sumMoney);
		cf0.setStatus("1");
		cashFlows.add(cf0);
		int length = list.size();
		for (int i = 1; i < length; i++) {
			CashFlow cashFlow = list.get(i);

			Float debit = cashFlow.getDebit();// 借方
			if (debit == null)
				debit = 0f;
			Float creditor = cashFlow.getCreditor();// 贷方
			if (creditor == null)
				creditor = 0f;
			sumMoney = sumMoney - debit + creditor;
			System.out.println(cashFlow + "\t" + sumMoney);
			cashFlow.setSettle(sumMoney);
			cashFlow.setStatus("1");
			cashFlows.add(cashFlow);
		}
		cashFlowDao.batchModifyCashFlow(cashFlows);
	}

	public static void main(String[] args) {
		Float f = null;
		boolean ff = Float.isNaN(f);
		System.out.println(ff);
	}
}
